﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using XuanFe.Database.Model;

namespace XuanFe.Database.DAL
{
    public class NationDAL
    {
        private const string SQL_SELECT_WS_NATION = "select NAT_NATION_ID as NATION_ID,NAT_NATION_NAME as NATION_NAME,NAT_SORT_INDEX as SORT_INDEX,NAT_RCRE_STAFF_ID as RCRE_STAFF_ID,NAT_RCRE_TIME as RCRE_TIME,NAT_LCHG_STAFF_ID as LCHG_STAFF_ID,NAT_LCHG_TIME as LCHG_TIME from WS_NATION where 1=1";
        private const string SQL_INSERT_WS_NATION = "insert into WS_NATION(NAT_NATION_ID,NAT_NATION_NAME,NAT_SORT_INDEX,NAT_RCRE_STAFF_ID,NAT_RCRE_TIME) VALUES(@NationId,@NationName,@SortIndex,@LastStaffId,@LastTime)";
        private const string SQL_UPDATE_WS_NATION = "update WS_NATION set NAT_NATION_NAME=@NationName,NAT_SORT_INDEX=@SortIndex,NAT_LCHG_STAFF_ID=@LastStaffId,NAT_LCHG_TIME=@LastTime where 1=1";
        private const string SQL_DELETE_WS_NATION = "delete from WS_NATION where 1=1";
        private const string SQL_COUNT_WS_NATION = "select count(NAT_NATION_ID) from WS_NATION where 1=1";

        private const string SQL_WHERE_NATION_ID = " and NAT_NATION_ID=@NationId ";
        private const string SQL_WHERE_NATION_ID_NAME = " and NAT_NATION_ID<>@NationId and NAT_NATION_NAME=@NationName ";

        private const string SQL_ORDER_BY_SORT_INDEX = " order by SORT_INDEX ";

        private const string PARM_NATION_ID = "@NationId";
        private const string PARM_NATION_NAME = "@NationName";
        private const string PARM_SORT_INDEX = "@SortIndex";
        private const string PARM_LAST_STAFF_ID = "@LastStaffId";
        private const string PARM_LAST_TIME = "@LastTime";

        Nullable<DateTime> NullDateTime = null;
        private static NationDAL instance = new NationDAL();
        /// <summary>
        /// 初始化实例
        /// </summary>
        /// <returns></returns>
        public static NationDAL Instance
        {
            get
            {
                return instance;
            }
        }
        public DataSet GetDataSet()
        {
            StringBuilder sbSQL = new StringBuilder(SQL_SELECT_WS_NATION);
            sbSQL.Append(SQL_ORDER_BY_SORT_INDEX);

            return MySQLHelper.Instance.ExecuteDataSet(MySQLHelper.Instance.ConnectionString, CommandType.Text, sbSQL.ToString(), null);
        }
        public List<NationModel> Gets()
        {
            using (DataSet ds = GetDataSet())
            {
                DataTable dt = ds.Tables[0];
                List<NationModel> list = dt.AsEnumerable().Select(t => new NationModel
                {
                    Id = Convert.ToString(t.ItemArray[0]),
                    Name = Convert.ToString(t.ItemArray[1]),
                    SortIndex = Convert.ToInt32(t.ItemArray[2]),
                    CreatedStaffId = Convert.ToString(t.ItemArray[3]),
                    CreatedDate = Convert.ToDateTime(t.ItemArray[4]),
                    LastModifiedStaffId = t.IsNull(5) ? string.Empty : Convert.ToString(t.ItemArray[5]),
                    LastModifiedDate = t.IsNull(6) ? NullDateTime : Convert.ToDateTime(t.ItemArray[6])
                }).ToList<NationModel>();

                return list;
            }
        }
        public NationModel Get(string id)
        {
            StringBuilder sbSQL = new StringBuilder(SQL_SELECT_WS_NATION);
            sbSQL.Append(SQL_WHERE_NATION_ID);

            List<MySqlParameter> parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter(PARM_NATION_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = id });

            using (DataSet ds = MySQLHelper.Instance.ExecuteDataSet(MySQLHelper.Instance.ConnectionString, CommandType.Text, sbSQL.ToString(), parms.ToArray()))
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataRow t = ds.Tables[0].Rows[0];
                    return new NationModel()
                    {
                        Id = Convert.ToString(t.ItemArray[0]),
                        Name = Convert.ToString(t.ItemArray[1]),
                        SortIndex = Convert.ToInt32(t.ItemArray[2]),
                        CreatedStaffId = Convert.ToString(t.ItemArray[3]),
                        CreatedDate = Convert.ToDateTime(t.ItemArray[4]),
                        LastModifiedStaffId = t.IsNull(5) ? string.Empty : Convert.ToString(t.ItemArray[5]),
                        LastModifiedDate = t.IsNull(6) ? NullDateTime : Convert.ToDateTime(t.ItemArray[6])
                    };
                }
                return null;
            }
        }
        public int Insert(DbTransaction trans, NationModel model)
        {
            List<MySqlParameter> parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter(PARM_NATION_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.Id });
            parms.Add(new MySqlParameter(PARM_NATION_NAME, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.Name });
            parms.Add(new MySqlParameter(PARM_SORT_INDEX, MySqlDbType.Int32) { Direction = ParameterDirection.Input, Value = model.SortIndex });
            parms.Add(new MySqlParameter(PARM_LAST_STAFF_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.LastModifiedStaffId });
            parms.Add(new MySqlParameter(PARM_LAST_TIME, MySqlDbType.DateTime) { Direction = ParameterDirection.Input, Value = model.LastModifiedDate });

            return MySQLHelper.Instance.ExecuteNonQuery(trans, CommandType.Text, SQL_INSERT_WS_NATION, parms.ToArray());
        }
        public int Update(DbTransaction trans, NationModel model)
        {
            StringBuilder sbSQL = new StringBuilder(SQL_UPDATE_WS_NATION);
            sbSQL.Append(SQL_WHERE_NATION_ID);

            List<MySqlParameter> parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter(PARM_NATION_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.Id });
            parms.Add(new MySqlParameter(PARM_NATION_NAME, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.Name });
            parms.Add(new MySqlParameter(PARM_SORT_INDEX, MySqlDbType.Int32) { Direction = ParameterDirection.Input, Value = model.SortIndex });
            parms.Add(new MySqlParameter(PARM_LAST_STAFF_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.LastModifiedStaffId });
            parms.Add(new MySqlParameter(PARM_LAST_TIME, MySqlDbType.DateTime) { Direction = ParameterDirection.Input, Value = model.LastModifiedDate });

            return MySQLHelper.Instance.ExecuteNonQuery(trans, CommandType.Text, sbSQL.ToString(), parms.ToArray());
        }
        public int Delete(DbTransaction trans, string id)
        {
            StringBuilder sbSQL = new StringBuilder(SQL_DELETE_WS_NATION);
            sbSQL.Append(SQL_WHERE_NATION_ID);

            List<MySqlParameter> parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter(PARM_NATION_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = id });

            return MySQLHelper.Instance.ExecuteNonQuery(trans, CommandType.Text, sbSQL.ToString(), parms.ToArray());
        }
        public bool Exist(DbTransaction trans, string id)
        {
            StringBuilder sbSQL = new StringBuilder(SQL_COUNT_WS_NATION);
            sbSQL.Append(SQL_WHERE_NATION_ID);

            List<MySqlParameter> parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter(PARM_NATION_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = id });

            return MySQLHelper.Instance.RecordIsExist(trans, sbSQL.ToString(), parms.ToArray());
        }
        public bool Exist(string id)
        {
            StringBuilder sbSQL = new StringBuilder(SQL_COUNT_WS_NATION);
            sbSQL.Append(SQL_WHERE_NATION_ID);

            List<MySqlParameter> parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter(PARM_NATION_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = id });

            return MySQLHelper.Instance.RecordIsExist(MySQLHelper.Instance.ConnectionString, sbSQL.ToString(), parms.ToArray());
        }
        public bool Exist(string id, string name)
        {
            StringBuilder sbSQL = new StringBuilder(SQL_COUNT_WS_NATION);
            sbSQL.Append(SQL_WHERE_NATION_ID_NAME);

            List<MySqlParameter> parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter(PARM_NATION_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = id });
            parms.Add(new MySqlParameter(PARM_NATION_NAME, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = name });

            return MySQLHelper.Instance.RecordIsExist(MySQLHelper.Instance.ConnectionString, sbSQL.ToString(), parms.ToArray());
        }
    }
}
